package com.tenement.service.impl;

import com.google.gson.Gson;
import com.tenement.dao.*;
import com.tenement.domain.*;
import com.tenement.service.StreetService;
import com.tenement.utils.Tools;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @ClassName : StreetServiceImpl  //类名
 * @Description : ${description}  //描述
 * @Author : 易嵘  //作者
 * @Date: 2020-06-07 11:34  //时间
 */
@Service
public class StreetServiceImpl implements StreetService {

    @Resource
    TenementStreetMapper streetMapper;

    @Resource
    TenementLogMapper logMapper;

    @Resource
    TenementClientMapper clientMapper;

    @Resource
    TenementHouseMapper houseMapper;

    @Override
    public List<TenementStreet> findAll(TenementStreet street) {
        return streetMapper.findAll(street);
    }

    @Override
    public List<TenementStreet> selectAll(String sectorId) {
        return streetMapper.selectAll(sectorId);
    }

    @Override
    public Long overCount(TenementStreet street, String startDate, String endDate) {
        return streetMapper.overCount(street,startDate,endDate);
    }

    @Override
    public int add(TenementStreet street, TenementUser user, String ip) {
        street.setStreetId(UUID.randomUUID().toString().replaceAll("-",""));
        street.setUserId(user.getUserId());
        street.setUserName(user.getUserName());
        street.setCreatedBy(user.getUserName());
        street.setRevision(1);
        if(street.getStreetName()==0){ // 客户
            TenementClient client=new TenementClient();
            client.setClientId(street.getSectorId());
            client.setClientFollow(client.getClientFollow()+street.getSectorName());
            clientMapper.updateByPrimaryKeySelective(client);
        }else{ // 房源
            TenementHouse house=new TenementHouse();
            house.setHouseId(street.getSectorId());
            house.setHouseFollow(house.getHouseFollow()+street.getSectorName());
            houseMapper.updateByPrimaryKeySelective(house);
        }
        //street.setCreatedTime(new Date());
        /*if(street.getStreetName()==0){
            TenementClient client=new TenementClient();
            client.setClientId(street.getSectorId());
            client.setRemark(street.getSectorName());
            clientMapper.updateByPrimaryKeySelective(client);
        }else{
            TenementHouse house=new TenementHouse();
            house.setHouseId(street.getSectorId());
            house.setHouseParticular(street.getSectorName());
            tenementHouseMapper.updateByPrimaryKeySelective(house);
        }*/

        TenementLog log=new TenementLog();
        log.setLogId(UUID.randomUUID().toString().replaceAll("-",""));
        log.setLogTitle("新增跟进信息");
        StringBuffer logBody=new StringBuffer();
        Tools.logStreetMod(street,logBody);
        log.setLogBody(logBody.toString());
        log.setLogType(1);
        log.setLogIp(ip);
        log.setCreatedBy(user.getUserName());
        logMapper.insertSelective(log);

        return streetMapper.insertSelective(street);
    }

    @Override
    public int update(TenementStreet street, TenementUser user, String ip) {
        street.setUpdatedBy(user.getUserName());
        street.setUpdatedTime(new Date());

        TenementLog log=new TenementLog();
        log.setLogId(UUID.randomUUID().toString().replaceAll("-",""));
        log.setLogTitle("修改跟进信息");
        StringBuffer logBody=new StringBuffer();
        Tools.logStreetMod(street,logBody);
        log.setLogBody(logBody.toString());
        log.setLogType(4);
        log.setLogIp(ip);
        log.setCreatedBy(user.getUserName());
        logMapper.insertSelective(log);

        return streetMapper.updateByPrimaryKeySelective(street);
    }

    @Override
    public int deleteById(String id, TenementUser user, String ip) {
        TenementStreet street=streetMapper.selectByPrimaryKey(id);
        List<TenementStreet> streets=streetMapper.selectAll(street.getSectorId());
        StringBuffer follow=new StringBuffer();
        for (TenementStreet tenementStreet : streets) {
            follow.append(tenementStreet.getSectorName());
        }
        if(street.getStreetName()==0){ // 客户
            TenementClient client=new TenementClient();
            client.setClientId(street.getSectorId());
            client.setClientFollow(follow.toString());
            clientMapper.updateByPrimaryKeySelective(client);
        }else{ // 房源
            TenementHouse house=new TenementHouse();
            house.setHouseId(street.getSectorId());
            house.setHouseFollow(follow.toString());
            houseMapper.updateByPrimaryKeySelective(house);
        }


        TenementLog log=new TenementLog();
        log.setLogId(UUID.randomUUID().toString().replaceAll("-",""));
        log.setLogTitle("删除跟进信息");
        StringBuffer logBody=new StringBuffer();
        Tools.logStreetMod(street,logBody);
        log.setLogBody(logBody.toString());
        log.setLogType(2);
        log.setLogIp(ip);
        log.setCreatedBy(user.getUserName());
        logMapper.insertSelective(log);
        return streetMapper.deleteById(id);
    }

    @Override
    public int deleteAll(String[] ids, TenementUser user, String ip) {
        List<String> list=new ArrayList(Arrays.asList(ids));
        /*TenementLog log=new TenementLog();
        log.setLogId(UUID.randomUUID().toString().replaceAll("-",""));
        log.setLogTitle("修改跟进信息");
        log.setLogBody(new Gson().toJson(streetMapper.findAllId(list)));
        log.setLogType(4);
        log.setLogIp(ip);
        log.setCreatedBy(user.getUserName());
        logMapper.insertSelective(log);*/
        TenementLog log=new TenementLog();
        log.setLogId(UUID.randomUUID().toString().replaceAll("-",""));
        log.setLogTitle("批量删除跟进信息");
        StringBuffer logBody=new StringBuffer();
        Tools.logStreetMods(streetMapper.findAllId(list),logBody);
        log.setLogBody(logBody.toString());
        log.setLogType(2);
        log.setLogIp(ip);
        log.setCreatedBy(user.getUserName());
        logMapper.insertSelective(log);
        return streetMapper.deleteAll(list);
    }

    @Override
    public TenementStreet findById(String id) {
        return streetMapper.selectByPrimaryKey(id);
    }
}
